package com.lihaogn.sell.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lihaogn.sell.vo.SearchPageVo;
import com.lihaogn.sell.entity.Admin;
import com.lihaogn.sell.mapper.AdminMapper;

import com.lihaogn.sell.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;

/**
 * @FileName: AdminServiceImpl
 * @Description: TODO
 * @Author: lihao
 * @Date: 2019-04-22 20:38
 * @Version: 1.0
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Admin> getAllAdmin() {

        return adminMapper.queryAdmin();
    }

    @Override
    public Map<String, Object> queryAdminForPage(String page, String size) {

        // 存储返回结果
        Map<String, Object> modelMap = new HashMap<>();
        List<Admin> adminList;

        Map<String, Integer> pageMap = new HashMap<>();

        int pageOne = Integer.valueOf(page);
        int pageSize = Integer.valueOf(size);
        int start = (pageOne - 1) * pageSize;

        pageMap.put("page", start);
        pageMap.put("size", pageSize);

        try {
            adminList = adminMapper.queryAdminForPage(pageMap);
            modelMap.put("code", 0);
            modelMap.put("msg", "success");
            modelMap.put("data", adminList);
            modelMap.put("count", adminMapper.getRowsCount());
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.put("code", 1);
            modelMap.put("msg", e.toString());
        }

        return modelMap;
    }

    @Override
    public int insertAdmin(Admin admin) {

        admin.setAdminId(UUID.randomUUID().toString());

        return adminMapper.insertAdmin(admin);

    }

    @Override
    public int queryAdminByName(String adminName) {
        return adminMapper.queryAdminByName(adminName);
    }

    @Override
    public int updateAdmin(Admin admin) {
        return adminMapper.updateAdmin(admin);
    }

    @Override
    public Admin getAdminByName(String adminName) {
        return adminMapper.getAdminByName(adminName);
    }

    @Override
    public int deleteAdminByName(String adminName) {
        return adminMapper.deleteAdminByName(adminName);
    }

    @Override
    public int deleteAdminsInNames(HttpServletRequest request) {

        // 记录删除的个数
        int num;
        BufferedReader reader;

        try {
            reader = request.getReader();

            StringBuffer res = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                res.append(line);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            ArrayList<String> nameList = objectMapper.readValue(res.toString(), ArrayList.class);

            HashMap<String, Object> map = new HashMap<>();
            map.put("nameList", nameList);

            num = adminMapper.deleteAdminInNames(map);
            if (num == nameList.size()) {
                return 1;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Map<String, Object> searchAdmin(SearchPageVo searchPageVo) {

        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        
        List<Admin> adminList = null;
        int searchCount = 0;

        Integer limit = searchPageVo.getLimit();
        Integer page = (searchPageVo.getPage()-1)*limit;
        String username = searchPageVo.getName();
        String start = searchPageVo.getStart();
        String end = searchPageVo.getEnd();

        queryMap.put("page", page);
        queryMap.put("size", limit);

        if (!"".equals(username)) {
            // username不为空，按照username查找
            Admin admin = adminMapper.getAdminByName(username);
            adminList = new ArrayList<>();
            adminList.add(admin);

            resultMap.put("code", 0);
            resultMap.put("msg", "success");
            resultMap.put("count",1 );
            resultMap.put("data",adminList);

            return resultMap;
        } else if ("".equals(start)) {
            // 开始日期为空，则查找结束日期前的
            queryMap.put("end", end);
            adminList = adminMapper.searchAdminByDateForPage(queryMap);
            searchCount = adminMapper.searchCountByDate(queryMap);
        } else if ("".equals(end)) {
            // 结束日期为空，则查找开始日期后的
            queryMap.put("start", start);
            adminList = adminMapper.searchAdminByDateForPage(queryMap);
            searchCount = adminMapper.searchCountByDate(queryMap);
        } else {
            // 查找开始日期到结束日期的
            queryMap.put("end", end);
            queryMap.put("start", start);
            adminList = adminMapper.searchAdminByDateForPage(queryMap);
            searchCount = adminMapper.searchCountByDate(queryMap);
        }

        resultMap.put("code", 0);
        resultMap.put("msg", "success");
        resultMap.put("count", searchCount);
        resultMap.put("data",adminList);

        return resultMap;
    }

    @Override
    public Admin getAdminById(String adminIdTmp) {

        return adminMapper.queryAdminById(adminIdTmp);
    }


}
